package com.cc.mall.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.common.utils.PageUtils;
import com.cc.common.utils.R;
import com.cc.common.utils.ResponseData;
import com.cc.mall.entity.ProductDetailEntity;
import com.cc.mall.entity.RotationChartEntity;
import com.cc.mall.service.ProductDetailService;
import com.cc.mall.service.RotationChartService;
import com.cc.mall.vo.Cart;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mall/rotationchart")
@Api(tags = "后台轮播图")
public class LunboChartController {

    @Autowired
    private RotationChartService rotationChartService;

    @GetMapping("/getAllRotationchart")
    @ApiOperation("后台获取所有的轮播图")
    public ResponseData<Page<RotationChartEntity>> getAllRotationchart(@RequestParam(required = false) Map<String,Object> params) {
        Page<RotationChartEntity> page = rotationChartService.getAllRotationchart(params);
        return ResponseData.success(page);
    }

    @PostMapping("/removeRotationchart")
    @ApiOperation("根据id删除轮播图")
    public R removeRotationchart(@RequestParam(required = true) String id) {
        rotationChartService.remove(new QueryWrapper<RotationChartEntity>().eq("id",id));
        return R.ok();
    }

    @PostMapping("/changeRotationchart")
    @ApiOperation("根据id改变轮播图播出状态")
    public R changeRotationchart(@RequestParam(required = true) Long id,@RequestParam(required = true) int status) {
        RotationChartEntity rotationChartEntity = new RotationChartEntity();
        rotationChartEntity.setId(id);
        if (status == 1) rotationChartEntity.setStatus(0);
        else if (status == 0) rotationChartEntity.setStatus(1);
        else return R.error();

        rotationChartService.updateById(rotationChartEntity);
        return R.ok();
    }

    @PostMapping("/changeRotationchartUrl")
    @ApiOperation("根据id改变轮播图地址")
    public R changeRotationchartUrl(@RequestParam(required = true) Long id,@RequestParam(required = true) String addr) {
        RotationChartEntity rotationChartEntity = new RotationChartEntity();
        rotationChartEntity.setId(id);
        rotationChartEntity.setRotationChart(addr);
        rotationChartService.updateById(rotationChartEntity);
        return R.ok();
    }

    @PostMapping("/insertRotationcharts")
    @ApiOperation("添加轮播图List")
    @ApiImplicitParam(name = "lunboUrls", value = "轮播图List", paramType = "query", dataType = "List<String>", required = true)
    public R insertRotationcharts(@RequestBody List<String> lunboUrls) {
        List<RotationChartEntity> rotationCharts = new ArrayList<>();
        for (String lunboUrl:lunboUrls){
            RotationChartEntity rotationChartEntity = new RotationChartEntity();
            rotationChartEntity.setRotationChart(lunboUrl);
            rotationChartEntity.setStatus(0);
            rotationCharts.add(rotationChartEntity);
        }
        rotationChartService.saveBatch(rotationCharts);
        return R.ok();
    }
}
